package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.how2java.pojo.Category;

public class TestMybatis {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        get(session);//selectOne
        listAll(session);//selectAll


        add(session);//insert
        delete(session);//delete
        update(session);//update


        session.commit();//如果是修改(update/delete/insert)操作必须要commit
        session.close();

    }

    private static void update(SqlSession session) {
        Category c = session.selectOne("getCategory", 2);
        c.setName("ÐÞ¸ÄÁËµÄCategoryÃû·Q");
        session.update("updateCategory", c);

        listAll(session);
    }

    private static void get(SqlSession session) {
        Category c = session.selectOne("getCategory", 2);

        System.out.println(c.getName());
    }

    private static void delete(SqlSession session) {
        Category c = new Category();
        c.setId(6);
        session.delete("deleteCategory", c);
        listAll(session);
    }

    private static void add(SqlSession session) {
        Category c = new Category();
        c.setName("ÐÂÔö¼ÓµÄCategory");
        session.insert("addCategory", c);

        listAll(session);
    }

    private static void listAll(SqlSession session) {
        List<Category> cs = session.selectList("listCategory");
        for (Category c : cs) {
            System.out.println(c.getName());
        }
    }
}